﻿using System;
using System.IO;
using System.Text;

namespace ebay.Utility
{
    public class FileLog
    {
        public static void CreateLog(LogAction l, string ebayUserId, string Msg)
        {
            string dir = AppDomain.CurrentDomain.SetupInformation.ApplicationBase + "\\Log\\" + DateTime.Now.ToString("yyyy\\\\MM\\\\dd") + "\\" + ebayUserId;
            string fileName = dir;
            switch (l)
            {
                case LogAction.ActiveListing:
                    fileName += "\\ActiveListing.txt";
                    break;
                case LogAction.Fees:
                    fileName += "\\EbayFee.txt";
                    break;
                case LogAction.Order:
                    fileName += "\\Order.txt";
                    break;
                case LogAction.Error:
                    fileName += "\\Error.txt";
                    break;
                case LogAction.OrderCheck:
                    fileName += "\\OrderCheck.txt";
                    break;
                case LogAction.PayPal:
                    fileName += "\\PayPal.txt";
                    break;
            }
            if (!Directory.Exists(dir))
            {
                Directory.CreateDirectory(dir);
            }
            if (!File.Exists(fileName))
            {
                File.Create(fileName).Close();
            }
            using (StreamWriter sw = new StreamWriter(fileName, true, Encoding.UTF8))
            {
                sw.WriteLine("【" + System.DateTime.Now.ToString() + "】：\t" + Msg);
                sw.WriteLine(Environment.NewLine);
                sw.Close();
            }
        }

        public static void CreateErrorLog(string Msg)
        {
            String dir = AppDomain.CurrentDomain.SetupInformation.ApplicationBase + "\\Log\\" + DateTime.Now.ToString("yyyy\\\\MM\\\\dd");
            string fileName = dir + "\\Error.txt";
            
            if (!Directory.Exists(dir))
            {
                Directory.CreateDirectory(dir);
            }
            
            if (!File.Exists(fileName))
            {
                File.Create(fileName).Close();
            }
            
            using (StreamWriter sw = new StreamWriter(fileName, true, Encoding.UTF8))
            {
                sw.WriteLine("【" + System.DateTime.Now.ToString() + "】：\t" + Msg);
                sw.WriteLine(Environment.NewLine);
                sw.Close();
            }
        }
    }
}
